package com.briup.product_source.web.controller;

import com.briup.product_source.aop.Logging;
import com.briup.product_source.bean.Basic.BaseAccount;
import com.briup.product_source.service.AccountService;
import com.briup.product_source.util.Result;
import com.briup.product_source.util.ResultUtil;
import com.google.common.collect.ImmutableMap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "登录模块")
@Slf4j
@RestController
public class LoginController {
    @Autowired
    private AccountService accountService;

    @ApiOperation("用户登录接口")
    @Logging
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",required = true,paramType = "form"),
            @ApiImplicitParam(name = "password",value = "密码",required = true,paramType = "form")
    })
    @PostMapping(value = "/login",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Result<Map<String,String>> login(@RequestParam String username, @RequestParam String password){
        log.info("用户名{},密码{}",username,password);
        String token = accountService.login(username, password);
        return ResultUtil.success(ImmutableMap.of("token",token));
    }

    @ApiOperation("退出登录接口")
    @Logging
    @GetMapping("/logout")
    public Result logout(){
        return ResultUtil.success();
    }

    @ApiOperation("获取当前登录用户信息接口")
    @GetMapping("/me")
    public Result getMe(HttpServletRequest request){
        String token = request.getHeader("token");
        BaseAccount account = accountService.findLoginUser(token);
        return ResultUtil.success(account);
    }
}
